package com.nari.ncps.screen.controller;

import com.nari.ncps.screen.common.pojo.ApiResponse;
import com.nari.ncps.screen.service.al.*;
import com.nari.ncps.screen.service.wholeproduct.QtDpIndustryProfileService;
import com.nari.ncps.screen.vo.al.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 能源消费表 前端控制器
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Tag(name = "铝业产业", description = "铝业产业")
@RestController
@RequestMapping("/al")
public class AlContrroller {

    @Resource
    private AlProdoperPrdSitService alProdoperPrdSitService;

    @Resource
    private AlProdoperPrdQualService alProdoperPrdQualService;

    @Resource
    private AlProdoperStockService alProdoperStockService;

    @Resource
    private AlProdoperStationPerService alProdoperStationPerService;

    @Resource
    private AlProdoperSalesService alProdoperSalesService;

    @Resource
    private AlOverviewItemService alOverviewItemService;

    @Resource
    private AlOpermonEqpStateService alOpermonEqpStateService;

    @Resource
    private AlSafetyenvAlarmService alSafetyenvAlarmService;

    @Resource
    private AlOpermonIuStateService alOpermonIuStateService;

    @Resource
    AlOpermonEnergyConAllService alOpermonEnergyConAllService;

    @Resource
    private QtDpIndustryProfileService industryProfileService;

    @Resource
    private AlRealtimePollutantService alRealtimePollutantService;


    @Operation(summary = "生产经营-生产情况-铝液生产情况", description = "获取铝液生产情况数据，包含月度计划完成量、实际完成量、完成率等指标")
    @GetMapping(value = "/prodoper/production/al-liquid")
    public ApiResponse<List<AlProdoperPrdSitVO>> getAluminumLiquidProduction(
            @RequestParam(value = "year", required = false) Integer year) {
        // 使用当前年份或传入的年份
        int currentYear = year != null ? year : LocalDate.now().getYear();

        // 查询铝液生产数据，product_type=0表示铝液
        List<AlProdoperPrdSitVO> productionData = alProdoperPrdSitService.getRecentSixMonthsData(currentYear, (byte) 0);
        return ApiResponse.succeed(productionData);
    }

    @Operation(summary = "生产经营-生产情况-铝锭生产情况", description = "生产经营-生产情况-铝锭生产情况")
    @GetMapping(value = "/prodoper/production/al-ingot")
    public ApiResponse<List<AlProdoperPrdSitVO>> m2(
            @RequestParam(value = "year", required = false) Integer year) {
        // 使用当前年份或传入的年份
        int currentYear = year != null ? year : LocalDate.now().getYear();

        // 查询铝锭生产数据，product_type=1表示铝锭
        List<AlProdoperPrdSitVO> productionData = alProdoperPrdSitService.getRecentSixMonthsData(currentYear, (byte) 1);
        return ApiResponse.succeed(productionData);
    }

    @Operation(summary = "生产经营-生产情况-碳素阳极生产情况", description = "生产经营-生产情况-碳素阳极生产情况")
    @GetMapping(value = "/prodoper/production/carbon-anode")
    public ApiResponse<List<AlProdoperPrdSitVO>> m3(
            @RequestParam(value = "year", required = false) Integer year) {
//        // 使用当前年份或传入的年份
//        int currentYear = year != null ? year : LocalDate.now().getYear();

        // 查询碳素阳极生产数据，product_type=2表示碳素阳极
        List<AlProdoperPrdSitVO> productionData = alProdoperPrdSitService.getRecentSixMonthsData(year, (byte) 2);
        return ApiResponse.succeed(productionData);
    }

    @Operation(summary = "生产经营-生产质量-铝产量质量", description = "M-获取铝液生产质量数据，包含当期品率和同期品率等指标")
    @GetMapping(value = "/prodoper/quality/al-yield")
    public ApiResponse<List<AlProdoperPrdQualOneVO>> getAluminumQuality(@RequestParam(value = "year", required = false) Integer year) {
        // 查询铝产量质量数据 MOCK
        List<AlProdoperPrdQualOneVO> qualityData = alProdoperPrdQualService.getAluminumQualityData();
        return ApiResponse.succeed(qualityData);
    }

    @Operation(summary = "生产经营-生产质量-低铁铝率", description = "M-生产经营-生产质量-低铁铝率")
    @GetMapping(value = "/prodoper/quality/low-iron-rate")
    public ApiResponse<List<AlProdoperPrdQualTwoVO>> m5(@RequestParam(value = "year", required = false) Integer year) {
        // 查询低铁铝率数据 MOCK
        List<AlProdoperPrdQualTwoVO> qualityData = alProdoperPrdQualService.getLowIronAluminumRateData();
        return ApiResponse.succeed(qualityData);
    }

    @Operation(summary = "生产经营-生产质量-碳素阳极质量", description = "M-生产经营-生产质量-碳素阳极质量")
    @GetMapping(value = "/prodoper/quality/anode-quality")
    public ApiResponse<List<AlProdoperPrdQualThreeVO>> m6(@RequestParam(value = "year", required = false) Integer year) {
        // 查询碳素阳极质量数据 MOCK
        List<AlProdoperPrdQualThreeVO> qualityData = alProdoperPrdQualService.getCarbonAnodeQualityData(year);
        return ApiResponse.succeed(qualityData);
    }

    @Operation(summary = "生产经营-库存情况-铝合金产品存量", description = "M-获取铝合金产品库存数据，包含最近12个月的库存量变化趋势")
    @GetMapping(value = "/prodoper/inventory/alloy-stock")
    public ApiResponse<List<AlProdoperStockVO>> getAlloyStockData(@RequestParam(value = "year", required = false) Integer year) {
        // 查询铝合金产品库存数据 MOCK
        List<AlProdoperStockVO> stockData = alProdoperStockService.getAlloyStockData();
        return ApiResponse.succeed(stockData);
    }

    @Operation(summary = "生产经营-库存情况-霍林河/白音华绿电火电占比", description = "获取霍林河和白音华电厂绿电火电占比数据，展示12个月的清洁能源使用情况")
    @GetMapping(value = "/prodoper/inventory/power-ratio")
    public ApiResponse<List<AlProdoperStationPerVO>> getPowerStationRatio(@RequestParam(value = "year", required = false) Integer year) {
        // 查询电厂绿电火电占比数据 M
        List<AlProdoperStationPerVO> powerRatioData = alProdoperStationPerService.getPowerStationRatio();
        return ApiResponse.succeed(powerRatioData);
    }


    @Operation(summary = "生产经营-销售情况-铝液销售情况", description = "获取铝液销售情况数据，包含12个月的计划销售量、实际销售量、完成率等关键指标")
    @GetMapping(value = "/prodoper/sales/al-liquid")
    public ApiResponse<List<AlProdoperSalesVO>> getLiquidSales(@RequestParam(value = "year", required = false) Integer year) {
        // 查询铝液销售数据 M
        List<AlProdoperSalesVO> salesData = alProdoperSalesService.getLiquidSalesData();
        return ApiResponse.succeed(salesData);
    }

    @Operation(summary = "生产经营-销售情况-铝锭销售情况", description = "生产经营-销售情况-铝锭销售情况")
    @GetMapping(value = "/prodoper/sales/al-ingot")
    public ApiResponse<List<AlProdoperSalesVO>> getAlIngotSales(@RequestParam(value = "year", required = false) Integer year) {
        // 查询铝锭销售数据 M
        List<AlProdoperSalesVO> salesData = alProdoperSalesService.getIngotSalesData();
        return ApiResponse.succeed(salesData);
    }

    @Operation(summary = "产业指标-综合技术指标看板", description = "获取铝业产业综合技术指标数据，包含铝产品年产量、电解铝年产能、炭素年产量等关键指标")
    @GetMapping(value = "/overview/tech-indicators")
    public ApiResponse<AlOverviewTechIndicatorsVO> getTechIndicatorsOverview(@RequestParam(value = "year", required = false) Integer year) {
        // 查询最新的综合技术指标数据 M
        AlOverviewTechIndicatorsVO techIndicators = alOverviewItemService.getLatestTechIndicators(year);
        return ApiResponse.succeed(techIndicators);
    }

    @Operation(summary = "运行监测-设备状态监测", description = "获取铝业产业各电厂设备状态数据，包含电解槽和整流机组的运行状态统计")
    @GetMapping(value = "/opermon/equipment-state")
    public ApiResponse<List<AlOpermonEquipmentStateVO>> getEquipmentStateMonitoring(@RequestParam(value = "year", required = false) Integer year) {
        // 查询所有铝厂的最新设备状态数据 M
        List<AlOpermonEquipmentStateVO> equipmentStateList = alOpermonEqpStateService.getLatestEquipmentState();
        return ApiResponse.succeed(equipmentStateList);
    }

    @Operation(summary = "运行监测-能源消费监测", description = "运行监测-能源消费监测")
    @GetMapping(value = "/opermon/energy-consumption")
    public ApiResponse<List<AlOpermonEnergyConsumptionVO>> m13(@RequestParam(value = "year", required = false) Integer year) {
        // 查询能源消费监测数据 M
        List<AlOpermonEnergyConsumptionVO> result = alSafetyenvAlarmService.getEnergyConsumptionData();
        return ApiResponse.succeed(result);
    }

    @Operation(summary = "运行监测-能源消费监测", description = "运行监测-能源消费监测")
    @GetMapping(value = "/opermon/energy-con")
    public ApiResponse<List<AlOpermonEnergyConVO>> m13B(@RequestParam(value = "year", required = false) Integer year) {
        // 查询能源消费监测数据 M
        List<AlOpermonEnergyConVO> result = alSafetyenvAlarmService.getEnergyConData();
        return ApiResponse.succeed(result);
    }



    @Operation(summary = "运行监测-能源消费监测-右侧", description = "运行监测-能源消费监测-右侧")
    @GetMapping(value = "/opermon/energy-consumption-all")
    public ApiResponse<AlOpermonEnergyConsumeAllVO> energyConsumptionAll(@RequestParam(value = "year",
            required = false) Integer year) {
        // 查询能源消费监测数据 M
        AlOpermonEnergyConsumeAllVO result = alOpermonEnergyConAllService.getConsumeAll(year);
        return ApiResponse.succeed(result);
    }


    @Operation(summary = "安全环保-环保数据实时监测", description = "安全环保-环保数据实时监测")
    @GetMapping(value = "/safetyenv/env-indicators")
    public ApiResponse<AlSafetyenvEnvIndicatorsVO> m14(
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "month", required = false) Integer month,
            @RequestParam(value = "day", required = false) Integer day) {
        AlSafetyenvEnvIndicatorsVO vo = alSafetyenvAlarmService.getEnvIndicators(year, month, day);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铝厂简介", description = "铝厂简介")
    @GetMapping(value = "/aluminum/factory/intro")
    public ApiResponse<AluminumFactoryIntroVO> getAluminumFactoryIntro(@RequestParam(value = "orgCode", required = false) String orgCode) {
//        AluminumFactoryIntroVO vo = industryProfileService.getAluminumFactoryIntro(orgCode);
        return ApiResponse.succeed();
    }

    @Operation(summary = "铝厂生产情况", description = "铝厂生产情况")
    @GetMapping(value = "/aluminum/production/status")
    public ApiResponse<AluminumProductionStatusVO> getAluminumProductionStatus(@RequestParam(value = "orgCode", required = false) String orgCode) {
        AluminumProductionStatusVO vo = alProdoperPrdSitService.getAluminumProductionStatus(orgCode);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铝厂生产效率和能耗", description = "铝厂生产效率和能耗")
    @GetMapping(value = "/aluminum/production/efficiency")
    public ApiResponse<AluminumProductionEfficiencyVO> getAluminumProductionEfficiency(@RequestParam(value = "orgCode", required = false) String orgCode) {
        // 根据orgCode查询电流电压详情表获取生产效率和能耗数据
        AluminumProductionEfficiencyVO vo = alOpermonIuStateService.getProductionEfficiency(orgCode);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铝厂安全环保", description = "铝厂安全环保")
    @GetMapping(value = "/aluminum/safety-env")
    public ApiResponse<AluminumSafetyEnvVO> getAluminumSafetyEnv(@RequestParam(value = "orgCode", required = false) String orgCode) {
        AluminumSafetyEnvVO vo = alSafetyenvAlarmService.getAluminumSafetyEnv(orgCode);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铝厂生产效率(新)", description = "铝厂生产效率(新)")
    @GetMapping(value = "/aluminum/production/efficiency1")
    public ApiResponse<AlRealtimePollutantEfficiencyVO> getAlRealtimePollutantEfficiency(@RequestParam(value = "orgCode", required = true) String orgCode) {
        // 获取铝厂生产效率数据
        AlRealtimePollutantEfficiencyVO vo = alRealtimePollutantService.getAlRealtimePollutantEfficiency(orgCode);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铝厂能耗(新)", description = "铝厂能耗(新)")
    @GetMapping(value = "/aluminum/production/efficiency2")
    public ApiResponse<List<AlRealtimePollutantConsumVO>> getAlRealtimePollutantConsumption(@RequestParam(value = "orgCode", required = true) String orgCode) {
        // 获取铝厂能耗数据
        List<AlRealtimePollutantConsumVO> vo = alRealtimePollutantService.getAlRealtimePollutantConsum(orgCode);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铝厂安全环保(新)", description = "铝厂安全环保(新)")
    @GetMapping(value = "/aluminum/production/efficiency3")
    public ApiResponse<AlRealtimePollutantSafetyVO> getAluminumSafetyEnvNew(@RequestParam(value = "orgCode", required = false) String orgCode) {
        AlRealtimePollutantSafetyVO vo = alRealtimePollutantService.getAlRealtimePollutantSafety(orgCode);
        return ApiResponse.succeed(vo);
    }

}
